package com.fenghongzhang.day016;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import java.util.ArrayList;
import java.util.List;

public class MusicUtil {
    public static final  int ORDER = 0;
    public static final  int SINGE = 1;
    public static final  int RANDOM = 2;

    public static List<Music> getMusic(Context context){
        List<Music> list = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor query = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if(query != null){
            while(query.moveToNext()){
                String title = query.getString(query.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String artist = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String duartion = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String data = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DATA));
                String size = query.getString(query.getColumnIndex(MediaStore.Audio.Media.SIZE));
                String albumId = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

                String path = getPath(albumId, context);

                if(Integer.parseInt(duartion) > 1000*60){
                    Music music = new Music(title,artist,duartion,data,size,path);
                    list.add(music);
                }


            }
            return list;
        }
        return  null;
    }

    public static String getPath(String aid,Context context){
        Uri parse = Uri.parse("content://media/external/audio/albums/"+aid);
        ContentResolver contentResolver = context.getContentResolver();
        Cursor query = contentResolver.query(parse, null, null, null, null);
        if (query != null){
            if(query.moveToFirst()){
                String album_art = query.getString(query.getColumnIndex("album_art"));
                return album_art;
            }
            query.close();
        }
        return null;


    }

    //格式化时间的方法 //2:09
    public static String fmtTime(String s){ //19014 -> 2:50   19014 / 1000 /60 ":0" 19014 / 1000 % 60
        int i = Integer.parseInt(s);
        if(i / 1000 % 60 < 10){
            return i/1000 /60 +":0"+i/1000 % 60;
        }
        return i/1000 /60 +":"+i/1000 % 60;
    }

}
